//
//  QuotProdCellEdit.m
//  Trading365
//
//  Created by Jason on 12-2-29.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "QuotProdEdit.h"
#import "QuotEditCell.h"
#import "InfoType.h"
#import "QuotSelectList.h"

@implementation QuotProdEdit
@synthesize table;
@synthesize prodDetail;
@synthesize quotProdEditBlcok;
@synthesize sRow,sSection;


static NSMutableDictionary *prodEditDetail;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.table.backgroundColor=BACKGROUND_COLOR;
    
    NSArray *title_array = [[NSArray alloc] initWithObjects:@"产品名称",@"报价数量",@"报价单位",@"含税单价",@"利润率",@"增值税率",@"退税率",nil];
    self.InfoListName = title_array; 
    [title_array release];   
    
    NSMutableDictionary *tmp=[[NSMutableDictionary alloc] initWithCapacity:0];
    
    NSString *value0=[self.prodDetail objectForKey:@"product_name_ch"];
    if (value0 == nil)
        value0=@"";
    NSString *value1=[self.prodDetail objectForKey:@"quote_quantity"];
    if (value1 == nil)
        value1=@"0";
    NSString *value2=[self.prodDetail objectForKey:@"quote_unit"];
    if (value2 == nil)
        value2=@"";
    NSString *value3=[self.prodDetail objectForKey:@"purchase_unit_price"];
    if (value3 == nil)
        value3=@"0";
    NSString *value4=[self.prodDetail objectForKey:@"profit_rate"];
    if (value4 == nil)
        value4=@"0";
    NSString *value5=[self.prodDetail objectForKey:@"product_vat"];
    if (value5 == nil)
        value5=@"0";
    NSString *value6=[self.prodDetail objectForKey:@"product_bt"];
    if (value6 == nil)
        value6=@"0";
    NSString *value7=[self.prodDetail objectForKey:@"product_id"];
    if (value7 == nil)
        value7=@"0";
    NSString *value8=[self.prodDetail objectForKey:@"basic_unit"];
    if (value8 == nil)
        value8=@"";
    [tmp setObject:value0 forKey:@"0,0"];
    [tmp setObject:value1 forKey:@"0,1"];
    [tmp setObject:value2 forKey:@"0,2"];
    [tmp setObject:value3 forKey:@"0,3"];
    [tmp setObject:value4 forKey:@"0,4"];
    [tmp setObject:value5 forKey:@"0,5"];
    [tmp setObject:value6 forKey:@"0,6"];
    [tmp setObject:value7 forKey:@"product_id"];
    [tmp setObject:@"" forKey:@"quote_unit_id"];
    [tmp setObject:value8 forKey:@"basic_unit"];
    
    
    prodEditDetail = [[NSMutableDictionary alloc] initWithDictionary:tmp];
    [tmp release];
    
//    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
//                                   initWithTitle:@"返回" 
//                                   style:UIBarButtonItemStyleDone
//                                   target:self
//                                   action:@selector(goBack:)];
//    self.navigationItem.leftBarButtonItem = saveButton;
//    [saveButton release];
    
    UIButton *barRetBut = [[UIButton alloc] initWithFrame:CGRectMake(5.0, 27.0, 50.0, 30.0)];
    [barRetBut setTitle:@" 返回" forState:UIControlStateNormal];
    UIFont *font              = [UIFont systemFontOfSize:14];
    barRetBut.titleLabel.font = font;
    UIImage *barRetImageNormal = [UIImage imageNamed:PIC_MAIL_SWITCH_SELF_NORMAL];
    [barRetBut setBackgroundImage:barRetImageNormal forState:UIControlStateNormal];
    UIImage *barRetImagePressed = [UIImage imageNamed:PIC_MAIL_SWITCH_SELF_PRESS];
    [barRetBut setBackgroundImage:barRetImagePressed forState:UIControlStateHighlighted];
    [barRetBut addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem  =  [[UIBarButtonItem alloc]initWithCustomView:barRetBut];
    
    self.navigationItem.rightBarButtonItem = nil;

    tableFrame=self.table.frame;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ///////////////////////////    
    [self.table reloadData];
}


- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [table release];
    [prodDetail release];
    
    [super dealloc];
}
//

-(void)initQuotProdEditBlcok:(QuotProdEditBlcok)quotProdEditBlcok1
{
    self.quotProdEditBlcok = quotProdEditBlcok1;
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.    
    return [self.InfoListName count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = [indexPath row];
    NSUInteger section = 0;
    
    static NSString *CellIdentifier = @"QuotEditCell";
    
    QuotEditCell* cell = (QuotEditCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"QuotEditCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        
    }
 
    cell.row = row;
    cell.section = section;

    [cell initEditBeginBlcok:^(NSString *txtValue,int section1,int row1)
     {
         NSString *tmp_v=[NSString stringWithString:txtValue];
         NSString *tmp_k=[NSString stringWithFormat:@"%d,%d",section1,row1];
         [prodEditDetail setValue:tmp_v forKey:tmp_k];
//         NSLog(@"prodEditDetail=%@",prodEditDetail);
     }];
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    //allow edit
    cell.textValue.userInteractionEnabled=YES;
    cell.textValue.delegate = (id)self;
    cell.textValue.tag = row;
    
    NSString* text = @"";
    
    if (row < [self.InfoListName count]) 
    {
        text = [self.InfoListName objectAtIndex:row];
    }
    cell.labName.text = text;
    
    if (row == 0 || row == 5 || row == 6 ){
        cell.textValue.userInteractionEnabled=NO;
    }
    if (row == 2){
        cell.textValue.userInteractionEnabled=NO;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;
    }
    
    if ([prodEditDetail count]>0){
        cell.textValue.text = [prodEditDetail objectForKey:[NSString stringWithFormat:@"%d,%d",section,row]];
//        NSLog(@"prodEditDetail=%@",prodEditDetail);
    }
    
    return cell;    
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row=indexPath.row;
    
    if ( row == 2){       
        QuotSelectList *selectList = [[QuotSelectList alloc] init];
       
        selectList.section=0;
        selectList.row=row;
        
        [selectList SetInfoType:INFO_TYPE_QUOT_PROD_UNIT];
        [selectList setProductID:[prodEditDetail objectForKey:@"product_id"]];
        
        [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
            [prodEditDetail setValue:name forKey:key];
            [prodEditDetail setValue:code forKey:@"quote_unit_id"];
//            NSLog(@"prodEditDetail=%@",prodEditDetail);
        }];
        
        [self.navigationController pushViewController:selectList animated:YES];
        [selectList release];
        
        return;
    }
}


#pragma mark - text delegate
//该方法为点击输入文本框要开始输入是调用的代理方法：就是把view上移到能看见文本框的地方
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.40]; 
    self.table.frame = CGRectMake(tableFrame.origin.x,tableFrame.origin.y,tableFrame.size.width,tableFrame.size.height - 216 - 49-30);
    
    [self.table scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:textField.tag inSection:0]
                      atScrollPosition:UITableViewScrollPositionBottom
                              animated:YES];  
    
    [UIView commitAnimations];
    
}

//该方法为完成输入后要调用的代理方法：虚拟键盘隐藏后，要恢复到之前的文本框地方
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:0.40]; 
    self.table.frame = tableFrame;
    [UIView commitAnimations];
    
}

#pragma mark - MyFun

- (IBAction)goBack:(id)sender
{
//    NSMutableDictionary *tmp_ret=[[NSMutableDictionary alloc] initWithDictionary:prodEditDetail];
    NSMutableDictionary *tmp=[[NSMutableDictionary alloc] initWithCapacity:0];
    [tmp setObject:[prodEditDetail objectForKey:@"0,0"] forKey:@"product_name_ch"]; //0,0
    [tmp setObject:[prodEditDetail objectForKey:@"0,1"] forKey:@"quote_quantity"];    //0,1
    [tmp setObject:[prodEditDetail objectForKey:@"0,2"] forKey:@"quote_unit"];         //0,2
    [tmp setObject:[prodEditDetail objectForKey:@"0,3"] forKey:@"purchase_unit_price"]; //0,3
    [tmp setObject:[prodEditDetail objectForKey:@"0,4"] forKey:@"profit_rate"];     //0,4
    [tmp setObject:[prodEditDetail objectForKey:@"0,5"] forKey:@"product_vat"];       //0,5
    [tmp setObject:[prodEditDetail objectForKey:@"0,6"] forKey:@"product_bt"];       //0,5"];
    [tmp setObject:[prodEditDetail objectForKey:@"product_id"] forKey:@"product_id"];      
    [tmp setObject:[prodEditDetail objectForKey:@"quote_unit_id"] forKey:@"quote_unit_id"];
    [tmp setObject:[prodEditDetail objectForKey:@"basic_unit"] forKey:@"basic_unit"];

    if (self.quotProdEditBlcok != nil){
        self.quotProdEditBlcok(tmp,sRow);
    }
    [tmp release];

    [self.navigationController popViewControllerAnimated:YES];
}



@end
